﻿using System;
using FluentValidation;
using FluentValidation.Results;
using GiveAQuiz.Web.Models;

namespace GiveAQuiz.Web.Validation
{
  /// <summary>
  /// Validate the ResetPasswordViewModel
  /// </summary>
  public class ResetPasswordValidator : AbstractValidator<ResetPasswordViewModel>
  {
    /// <summary>
    /// Creates a new instance of the ResetPasswordValidator
    /// </summary>
    public ResetPasswordValidator()
    {
      // check user name for null or empty when email is not entered
      Custom(rp => 
        String.IsNullOrWhiteSpace(rp.UserName) && String.IsNullOrWhiteSpace(rp.Email) ? 
          new ValidationFailure("UserName", "Username or Email is required") : null);

      // check for valid email address
      RuleFor(rp => rp.Email)
        .EmailAddress()
        .When(rp => String.IsNullOrWhiteSpace(rp.UserName) && !String.IsNullOrWhiteSpace(rp.Email))
        .WithMessage("Email Address is invalid");
    }
  }
}